﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Web;
using System.IO;
using System.Data.SqlClient;

public partial class UploadCV : LutefiskPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get uID and figure out secret lyeConcentration because we're about to do something outside of the app
        // and don't want others being able to perform this.
        long uIDLong = long.Parse(CurrentMusician.UID);
        long lyeConcentrationLong = Lutefisk.GetSecretLyeConcentration(uIDLong);

        
        // If they dont have a CV;  load the external CV form and prompt them to upload one
        if (!CurrentMusician.HasCV)
        {
            externalCVForm.Src = Lutefisk.WEBSITE_URL + "ExternalCVForm.aspx?uID=" + uIDLong.ToString() + "&lyeConcentration=" + lyeConcentrationLong.ToString();
            
            deleteCVPanel.Visible = false;
            externalCVPanel.Visible = true;
        }
        // If they have a CV; show the Panel that allows them to delete it.
        else
        {
            externalCVPanel.Visible = false;
            deleteCVPanel.Visible = true;
        }

    }

    protected void editProfileClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "Form");
    }
    protected void editMultimediaClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "UploadMultimedia");
    }
    protected void editCurriculumVitaeClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "UploadCV");
    }

    protected void viewClick(object sender, EventArgs e)
    {
        // Navigate to the your cv.
        Session["uid"] = CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "CV");
    }
    protected void deleteClick(object sender, EventArgs e)
    {
        try
        {
            // Create a file object of the location where the PDF file should be
            FileInfo cvFile = new FileInfo(Server.MapPath("~/") + "PDFs/" + CurrentMusician.UID + ".pdf");
            
            // Make sure it exists.
            if (cvFile.Exists)
            {
                // Delete it
                File.Delete(Server.MapPath("~/") + "PDFs/" + CurrentMusician.UID + ".pdf");

                // Update musician's records.
                string sqlInsert = "UPDATE musicians SET cv = 0 WHERE uid = '" + CurrentMusician.UID + "' ;";

                //create an SQL connection.
                using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
                {
                    dbconn.Open();

                    //execute the command
                    using (SqlCommand cmd = new SqlCommand(sqlInsert, dbconn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException ax)
                        {
                            Console.WriteLine(ax);
                        }
                    }

                    Lutefisk.DirectPage(this, "UploadCV");
                }
                            



            }
            else
            {
                Lutefisk.ShowDialogBox(this.Page, "Internal Error", "This file does not seem to exist.");
            }

        }
        catch (Exception ex)
        { }

    }
}